package com.ruoyi.huanghefd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.huanghefd.domain.*;
import com.ruoyi.huanghefd.mapper.*;
import com.ruoyi.huanghefd.service.WorksheetService;
import com.ruoyi.huanghefd.utils.PageBean;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 工作票服务
 */
@Service
public class WorksheetServiceImpl extends ServiceImpl<WorksheetMapper, Worksheet>
        implements WorksheetService {
    private static final Logger log = LoggerFactory.getLogger(WorksheetServiceImpl.class);

    @Resource
    WorksheetMapper worksheetMapper;

    @Resource
    WorksheetSafetyMeasures1Mapper worksheetSafetyMeasures1Mapper;
    @Resource
    WorksheetSafetyMeasures2Mapper worksheetSafetyMeasures2Mapper;
    @Resource
    WorksheetSafetyMeasures3Mapper worksheetSafetyMeasures3Mapper;
    @Resource
    WorksheetSafetyMeasures4Mapper worksheetSafetyMeasures4Mapper;
    @Resource
    WorksheetSafetyMeasures5Mapper worksheetSafetyMeasures5Mapper;
    @Resource
    WorksheetSafetyMeasuresAddMapper worksheetSafetyMeasuresAddMapper;

    @Resource
    WorksheetDevTryMapper worksheetDevTryMapper;
    @Resource
    WorksheetDevRecoverMapper worksheetDevRecoverMapper;

    @Override
    public AjaxResult addWorksheet(Worksheet worksheets) {
        int result = 0;
        try {
            //工作票主键
            String strWsId = UUID.randomUUID().toString().replace("-", "");

            //工作票票号
            //pubCommon pubCommon = new pubCommon();
            //String strWsCode = pubCommon.getGenerateOrder(strWsTye,"ws");
            String strWsCode = this.getGenerateOrder(worksheets.getWsType());
            log.info("生成工作票号：" + strWsCode);
            worksheets.setWsCode(strWsCode);
            worksheets.setWsId(strWsId);

            //插入工作票主表
            log.info("新增工作票-主表开始>>" + worksheets);
            result = worksheetMapper.insertAll(worksheets);
            log.info("新增工作票-主表结束<<" + result);

            //插入工作票 安全措施1明细表
            if (!CollectionUtils.isEmpty(worksheets.getSafetyMeasures1())) {
                List<WorksheetSafetyMeasures1> wsm1List = new ArrayList<>();
                for (WorksheetSafetyMeasures1 wsdtls : worksheets.getSafetyMeasures1()) {
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    WorksheetSafetyMeasures1 wsm1 = new WorksheetSafetyMeasures1();
                    wsm1.setWsId(uuid);
                    wsm1.setWsCode(strWsCode);
                    wsm1.setWsOrder(wsdtls.getWsOrder());
                    wsm1.setSafetyMeasures(wsdtls.getSafetyMeasures());
                    wsm1.setSafetyResult(wsdtls.getSafetyResult());
                    wsm1.setUserid(wsdtls.getUserid());
                    wsm1.setUser(wsdtls.getUser());
                    wsm1.setContext(wsdtls.getContext());
                    wsm1List.add(wsm1);
                }
                log.info("新增工作票-安全措施1明细表开始>>" + wsm1List);
                result = worksheetSafetyMeasures1Mapper.insertBatch(wsm1List);
                log.info("新增工作票-安全措施1明细表结束<<" + result);
            }
            //插入工作票 安全措施2明细表
            if (!CollectionUtils.isEmpty(worksheets.getSafetyMeasures2())) {
                List<WorksheetSafetyMeasures2> wsm2List = new ArrayList<>();
                for (WorksheetSafetyMeasures2 wsdtls : worksheets.getSafetyMeasures2()) {
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    WorksheetSafetyMeasures2 wsm2 = new WorksheetSafetyMeasures2();
                    wsm2.setWsId(uuid);
                    wsm2.setWsCode(strWsCode);
                    wsm2.setWsOrder(wsdtls.getWsOrder());
                    wsm2.setSafetyMeasures(wsdtls.getSafetyMeasures());
                    wsm2.setSafetyResult(wsdtls.getSafetyResult());
                    wsm2.setUserid(wsdtls.getUserid());
                    wsm2.setUser(wsdtls.getUser());
                    wsm2.setContext(wsdtls.getContext());
                    wsm2List.add(wsm2);
                }
                log.info("新增工作票-安全措施2明细表开始>>" + wsm2List);
                result = worksheetSafetyMeasures2Mapper.insertBatch(wsm2List);
                log.info("新增工作票-安全措施1明细表结束<<" + result);
            }
            //插入工作票 安全措施3明细表
            if (!CollectionUtils.isEmpty(worksheets.getSafetyMeasures3())) {
                List<WorksheetSafetyMeasures3> wsm3List = new ArrayList<>();
                for (WorksheetSafetyMeasures3 wsdtls : worksheets.getSafetyMeasures3()) {
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    WorksheetSafetyMeasures3 wsm3 = new WorksheetSafetyMeasures3();
                    wsm3.setWsId(uuid);
                    wsm3.setWsCode(strWsCode);
                    wsm3.setWsOrder(wsdtls.getWsOrder());
                    wsm3.setSafetyMeasures(wsdtls.getSafetyMeasures());
                    wsm3.setSafetyResult(wsdtls.getSafetyResult());
                    wsm3.setUserid(wsdtls.getUserid());
                    wsm3.setUser(wsdtls.getUser());
                    wsm3.setContext(wsdtls.getContext());
                    wsm3List.add(wsm3);
                }
                log.info("新增工作票-安全措施3明细表开始>>" + wsm3List);
                result = worksheetSafetyMeasures3Mapper.insertBatch(wsm3List);
                log.info("新增工作票-安全措施3明细表结束<<" + result);
            }
            //插入工作票 安全措施4明细表
            if (!CollectionUtils.isEmpty(worksheets.getSafetyMeasures4())) {
                List<WorksheetSafetyMeasures4> wsm4List = new ArrayList<>();
                for (WorksheetSafetyMeasures4 wsdtls : worksheets.getSafetyMeasures4()) {
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    WorksheetSafetyMeasures4 wsm4 = new WorksheetSafetyMeasures4();
                    wsm4.setWsId(uuid);
                    wsm4.setWsCode(strWsCode);
                    wsm4.setWsOrder(wsdtls.getWsOrder());
                    wsm4.setSafetyMeasures(wsdtls.getSafetyMeasures());
                    wsm4.setSafetyResult(wsdtls.getSafetyResult());
                    wsm4.setUserid(wsdtls.getUserid());
                    wsm4.setUser(wsdtls.getUser());
                    wsm4.setContext(wsdtls.getContext());
                    wsm4List.add(wsm4);
                }
                log.info("新增工作票-安全措施4明细表开始>>" + wsm4List);
                result = worksheetSafetyMeasures4Mapper.insertBatch(wsm4List);
                log.info("新增工作票-安全措施4明细表结束<<" + result);
            }
            //插入工作票 安全措施5明细表
            if (!CollectionUtils.isEmpty(worksheets.getSafetyMeasures5())) {
                List<WorksheetSafetyMeasures5> wsm5List = new ArrayList<>();
                for (WorksheetSafetyMeasures5 wsdtls : worksheets.getSafetyMeasures5()) {
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    WorksheetSafetyMeasures5 wsm5 = new WorksheetSafetyMeasures5();
                    wsm5.setWsId(uuid);
                    wsm5.setWsCode(strWsCode);
                    wsm5.setWsOrder(wsdtls.getWsOrder());
                    wsm5.setSafetyMeasures(wsdtls.getSafetyMeasures());
                    wsm5.setSafetyResult(wsdtls.getSafetyResult());
                    wsm5.setUserid(wsdtls.getUserid());
                    wsm5.setUser(wsdtls.getUser());
                    wsm5.setContext(wsdtls.getContext());
                    wsm5List.add(wsm5);
                }
                log.info("新增工作票-安全措施5明细表开始>>" + wsm5List);
                result = worksheetSafetyMeasures5Mapper.insertBatch(wsm5List);
                log.info("新增工作票-安全措施5明细表结束<<" + result);
            }

            //插入工作票 补充的安全措施
            if (!CollectionUtils.isEmpty(worksheets.getSafetyMeasuresAdds())) {
                List<WorksheetSafetyMeasuresAdd> wsmList = new ArrayList<>();
                for (WorksheetSafetyMeasuresAdd wsdtls : worksheets.getSafetyMeasuresAdds()) {
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    WorksheetSafetyMeasuresAdd wsm = new WorksheetSafetyMeasuresAdd();
                    wsm.setWsId(uuid);
                    wsm.setWsCode(strWsCode);
                    wsm.setWsOrder(wsdtls.getWsOrder());
                    wsm.setAddSafetyMeasures(wsdtls.getAddSafetyMeasures());
                    wsm.setAddSafetyResult(wsdtls.getAddSafetyResult());
                    wsm.setUserid(wsdtls.getUserid());
                    wsm.setUser(wsdtls.getUser());
                    wsmList.add(wsm);
                }
                log.info("新增工作票-补充的安全措施明细表开始>>" + wsmList);
                result = worksheetSafetyMeasuresAddMapper.insertBatch(wsmList);
                log.info("新增工作票-补充的安全措施明细表结束<<" + result);
            }

            //插入工作票 设备试运行明细表
            if (!CollectionUtils.isEmpty(worksheets.getDevTrys())) {
                List<WorksheetDevTry> wsdtList = new ArrayList<WorksheetDevTry>();
                for (WorksheetDevTry wsdtl : worksheets.getDevTrys()) {
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    WorksheetDevTry wsdt = new WorksheetDevTry();
                    wsdt.setWsId(uuid);
                    wsdt.setWsCode(strWsCode);
                    wsdt.setTryTime(wsdtl.getTryTime());
                    wsdt.setWsLicensorid(wsdtl.getWsLicensorid());
                    wsdt.setWsLicensor(wsdtl.getWsLicensor());
                    wsdt.setWsPrincipalid(wsdtl.getWsPrincipalid());
                    wsdt.setWsPrincipal(wsdtl.getWsPrincipal());
                    wsdt.setWsOrder(wsdtl.getWsOrder());

                    wsdtList.add(wsdt);
                }
                log.info("新增工作票-设备试运行细表开始>>" + wsdtList);
                result = worksheetDevTryMapper.insertBatch(wsdtList);
                log.info("新增工作票-设备试运行明细表结束<<" + result);
            }

            //插入工作票 设备恢复运行明细表
            if (!CollectionUtils.isEmpty(worksheets.getDevRecovers())) {
                List<WorksheetDevRecover> wsdrList = new ArrayList<WorksheetDevRecover>();
                for (WorksheetDevRecover wsdtl : worksheets.getDevRecovers()) {
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    WorksheetDevRecover wsdr = new WorksheetDevRecover();
                    wsdr.setWsId(uuid);
                    wsdr.setWsCode(strWsCode);
                    wsdr.setRecoverTime(wsdtl.getRecoverTime());
                    wsdr.setWsLicensorid(wsdtl.getWsLicensorid());
                    wsdr.setWsLicensor(wsdtl.getWsLicensor());
                    wsdr.setWsPrincipalid(wsdtl.getWsPrincipalid());
                    wsdr.setWsPrincipal(wsdtl.getWsPrincipal());
                    wsdr.setWsOrder(wsdtl.getWsOrder());

                    wsdrList.add(wsdr);
                }
                log.info("新增工作票-设备恢复运行细表开始>>" + wsdrList);
                result = worksheetDevRecoverMapper.insertBatch(wsdrList);
                log.info("新增工作票-设备恢复试运行明细表结束<<" + result);
            }

            return AjaxResult.success(strWsCode);
        } catch (Exception e) {
            return AjaxResult.error("工作票新增失败", e);
        }
    }

    @Override
    public AjaxResult delWorksheet(String strWsCode) {
        int result = 0;
        try {
            log.info("删除工作票-主表开始>>");
            result = worksheetMapper.delByWsId(strWsCode);
            log.info("删除工作票-主表结束<<" + result);

            log.info("删除工作票-安全措施1子表开始>>");
            result = worksheetSafetyMeasures1Mapper.delByWsCode(strWsCode);
            log.info("删除工作票-安全措施1子表结束<<" + result);

            log.info("删除工作票-安全措施2子表开始>>");
            result = worksheetSafetyMeasures2Mapper.delByWsCode(strWsCode);
            log.info("删除工作票-安全措施2子表结束<<" + result);

            log.info("删除工作票-安全措施3子表开始>>");
            result = worksheetSafetyMeasures3Mapper.delByWsCode(strWsCode);
            log.info("删除工作票-安全措施3子表结束<<" + result);

            log.info("删除工作票-安全措施4子表开始>>");
            result = worksheetSafetyMeasures4Mapper.delByWsCode(strWsCode);
            log.info("删除工作票-安全措施4子表结束<<" + result);

            log.info("删除工作票-安全措施5子表开始>>");
            result = worksheetSafetyMeasures5Mapper.delByWsCode(strWsCode);
            log.info("删除工作票-安全措施5子表结束<<" + result);

            log.info("删除工作票-补充安全措施子表开始>>");
            result = worksheetSafetyMeasuresAddMapper.delByWsCode(strWsCode);
            log.info("删除工作票-补充安全措施子表结束<<" + result);

            log.info("删除工作票-设备试运行子表开始>>");
            result = worksheetDevTryMapper.delByWsCode(strWsCode);
            log.info("删除工作票-设备试运行子表结束<<" + result);

            log.info("删除工作票-设备恢复运行子表开始>>");
            result = worksheetDevRecoverMapper.delByWsCode(strWsCode);
            log.info("删除工作票-设备恢复运行子表结束<<" + result);

            return AjaxResult.success(strWsCode);
        } catch (Exception e) {
            return AjaxResult.error("工作票删除失败", e);
        }
    }

    @Override
    public AjaxResult uptWorksheet(Worksheet worksheets) {
        int result = 0;
        try {
            log.info("修改工作票-主表开始>>");
            result = worksheetMapper.updateByWsId(worksheets);
            log.info("修改工作票-主表结束<<" + result);

            // 工作票 安全措施1
            if (!CollectionUtils.isEmpty(worksheets.getSafetyMeasures1())) {
                log.info("修改工作票-安全措施1子表开始>>");
                //最好的方式还是先删后插
                worksheetSafetyMeasures1Mapper.delByWsCode(worksheets.getWsCode());
                worksheetSafetyMeasures1Mapper.insertBatch(worksheets.getSafetyMeasures1());
                log.info("修改工作票-安全措施1子表结束<<" + result);
            }
            // 工作票 安全措施2
            if (!CollectionUtils.isEmpty(worksheets.getSafetyMeasures2())) {
                log.info("修改工作票-安全措施2子表开始>>");
                //最好的方式还是先删后插
                worksheetSafetyMeasures2Mapper.delByWsCode(worksheets.getWsCode());
                worksheetSafetyMeasures2Mapper.insertBatch(worksheets.getSafetyMeasures2());
                log.info("修改工作票-安全措施2子表结束<<" + result);
            }
            // 工作票 安全措施3
            if (!CollectionUtils.isEmpty(worksheets.getSafetyMeasures3())) {
                log.info("修改工作票-安全措施3子表开始>>");
                //最好的方式还是先删后插
                worksheetSafetyMeasures3Mapper.delByWsCode(worksheets.getWsCode());
                worksheetSafetyMeasures3Mapper.insertBatch(worksheets.getSafetyMeasures3());
                log.info("修改工作票-安全措施3子表结束<<" + result);
            }
            // 工作票 安全措施4
            if (!CollectionUtils.isEmpty(worksheets.getSafetyMeasures4())) {
                log.info("修改工作票-安全措施4子表开始>>");
                //最好的方式还是先删后插
                worksheetSafetyMeasures4Mapper.delByWsCode(worksheets.getWsCode());
                worksheetSafetyMeasures4Mapper.insertBatch(worksheets.getSafetyMeasures4());
                log.info("修改工作票-安全措施4子表结束<<" + result);
            }
            // 工作票 安全措施5
            if (!CollectionUtils.isEmpty(worksheets.getSafetyMeasures5())) {
                log.info("修改工作票-安全措施5子表开始>>");
                //最好的方式还是先删后插
                worksheetSafetyMeasures5Mapper.delByWsCode(worksheets.getWsCode());
                worksheetSafetyMeasures5Mapper.insertBatch(worksheets.getSafetyMeasures5());
                log.info("修改工作票-安全措施5子表结束<<" + result);
            }
            // 工作票 补充安全措施
            if (!CollectionUtils.isEmpty(worksheets.getSafetyMeasuresAdds())) {
                log.info("修改工作票-补充安全措施子表开始>>");
                //最好的方式还是先删后插
                worksheetSafetyMeasuresAddMapper.delByWsCode(worksheets.getWsCode());
                worksheetSafetyMeasuresAddMapper.insertBatch(worksheets.getSafetyMeasuresAdds());
                log.info("修改工作票-补充安全措施子表结束<<" + result);
            }

            // 工作票 检修设备试运行
            if (!CollectionUtils.isEmpty(worksheets.getDevTrys())) {
                log.info("修改工作票-检修设备试运行子表开始>>");
                //最好的方式还是先删后插
                worksheetDevTryMapper.delByWsCode(worksheets.getWsCode());
                worksheetDevTryMapper.insertBatch(worksheets.getDevTrys());
                log.info("修改工作票-检修设备试运行子表结束<<" + result);
            }
            // 工作票 检修设备恢复运行
            if (!CollectionUtils.isEmpty(worksheets.getDevRecovers())) {
                log.info("修改工作票-检修设备恢复运行子表开始>>");
                //最好的方式还是先删后插
                worksheetDevRecoverMapper.delByWsCode(worksheets.getWsCode());
                worksheetDevRecoverMapper.insertBatch(worksheets.getDevRecovers());
                log.info("修改工作票-检修设备恢复运行子表结束<<" + result);
            }
            return AjaxResult.success(worksheets.getWsCode());
        } catch (Exception e) {
            return AjaxResult.error("工作票修改失败", e);
        }
    }

    @Override
    public AjaxResult queryWorksheets(String strWsCode, String strWsDept, String strWsState, String strUserId, String strMailType) {
        List<Worksheet> worksheetList = worksheetMapper.queryWorksheetList(strWsCode, strWsDept, strWsState, strUserId, strMailType);
        if(CollectionUtils.isEmpty(worksheetList)){
            return AjaxResult.success("工作票查询为空！");
        }
        return AjaxResult.success(worksheetList);
    }

    @Override
    public PageBean queryWorksheetByPage(String strWsCode, String strWsDept, String strWsState, String strUserId, String strMailType, String strWsType, int pageNum, int pageSize) {
        PageBean pageBean = new PageBean();
        if (pageNum > 0 && pageSize > 0) {
            int totalRecord = worksheetMapper.getWorksheetNum(strWsCode, strWsDept, strWsState, strUserId, strMailType,strWsType);
            pageBean = new PageBean(pageNum, pageSize, totalRecord);

            //从该行开始拿数据
            int startIndex = (pageNum - 1) * pageSize;
            pageBean.setStartIndex(startIndex);
            pageBean.setCode(200);

            //数据集合
            List<Worksheet> worksheetList = worksheetMapper.queryWorksheetListByPage(strWsCode, strWsDept, strWsState, strUserId, strMailType,strWsType,startIndex, pageSize);
            pageBean.setList(worksheetList);
            pageBean.setSuccessMsg("工作票查询成功！");
        } else {
            pageBean.setCode(500);
            pageBean.setFailMsg("工作票查询失败！");
        }
        return pageBean;
    }

    @Override
    public AjaxResult verifyCreateSheets(String userId) {
        int exeNum = worksheetMapper.getExeSheetNum(userId);
        if (exeNum == 0) {
            return AjaxResult.success("此负责人下不存在正在执行的票，允许新建其他票！");
        }
        return AjaxResult.error("此负责人下存在正在执行的票，不允许新建其他票！");
    }

    /**
     * @param Type
     * @return java.lang.String
     * @Author gnplum
     * @Description //工作票票号
     * @Date 14:56 2023/11/12
     **/
    private String getGenerateOrder(String Type) {

        log.info("获取票编号=" + Type);
        String strWsCode = "";
        int order = 0;
        try {

            order = worksheetMapper.getWsCodeNum(Type);
            log.info("order=" + order);
            //DQ1202311110001
            strWsCode = Type + new SimpleDateFormat("yyyyMMdd").format(new Date()) + String.format("%03d", order);

            log.info("strWsCode=" + strWsCode);

        } catch (Exception e) {
            strWsCode = "";
            //e.printStackTrace();
            throw e;
            //log.error(e.getCause()+e.getMessage());
        }
        return strWsCode;
    }
}




